package pl.edu.pk.mech.kkbus.repository;

import static pl.edu.pk.mech.kkbus.staticstrings.NameEntity.NAME_ENTITY_REFUELING;
import static pl.edu.pk.mech.kkbus.staticstrings.RefuelingProperty.REFUELING_CAPACITY;
import static pl.edu.pk.mech.kkbus.staticstrings.RefuelingProperty.REFUELING_COSTS;
import static pl.edu.pk.mech.kkbus.staticstrings.RefuelingProperty.REFUELING_DATE;
import static pl.edu.pk.mech.kkbus.staticstrings.RefuelingProperty.REFUELING_ID_BUS;

import java.util.ArrayList;
import java.util.List;

import pl.edu.pk.mech.kkbus.datastore.common.SynchronizeCommon;
import pl.edu.pk.mech.kkbus.model.Pojo;
import pl.edu.pk.mech.kkbus.model.Refueling;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class RefuelingDAO extends BaseDAO{

	@Override
	public void fillProperty(Pojo pojo, Entity entity) {
		Refueling refueling = (Refueling) pojo;
		  entity.setProperty(REFUELING_CAPACITY, refueling.getCapacity());
		  entity.setProperty(REFUELING_COSTS, refueling.getCosts());
		  entity.setProperty(REFUELING_DATE, refueling.getDate());
		  entity.setProperty(REFUELING_ID_BUS, refueling.getIdBus());
	}

	public List<Refueling> getAllRefueling() {
		  List<Refueling> temp = new ArrayList<Refueling>();
		  Iterable<Entity> entityList = SynchronizeCommon.listEntities(NAME_ENTITY_REFUELING, null, null);
		  for (Entity entity : entityList) {
			  temp.add(new Refueling(entity));
		  }
		  return temp;
	}
	
	public List<Refueling> getRefuelingByIdBus(String idBus) {
		  List<Refueling> temp = new ArrayList<Refueling>();
		  Iterable<Entity> entityList = SynchronizeCommon.listEntities(NAME_ENTITY_REFUELING, REFUELING_ID_BUS, idBus);
		  for (Entity entity : entityList) {
			  temp.add(new Refueling(entity));
		  }
		  return temp;
	}
	
	public Refueling getRefueling(long id) {
		  Key key = KeyFactory.createKey(NAME_ENTITY_REFUELING, id);
		  return new Refueling(SynchronizeCommon.findEntity(key));
	}
}
